﻿using System;
using System.Linq;
using System.Reflection;

namespace Dora.Interception
{
    public static class InterceptorRegistrationProviderExtensions
    {
        /// <summary>
        /// 拦截链中存在拦截器的目标方法，其方法的声明类型与type变量的类型相同，则表示可以对其拦截
        /// </summary>
        /// <param name="interceptorRegistrationProvider">拦截器的提供者</param>
        /// <param name="type">匹配类型</param>
        /// <returns>是否会拦截</returns>
        public static bool WillIntercept(this IInterceptorRegistrationProvider interceptorRegistrationProvider, Type type)
        {
            return interceptorRegistrationProvider.Registrations.Any(it => it.Target.DeclaringType == type);
        }

        /// <summary>
        /// 通过传入的MethodInfo对象，判断拦截链中是否存在目标方法与之一致
        /// </summary>
        /// <param name="interceptorRegistrationProvider">拦截器的提供者</param>
        /// <param name="methodInfo">传递的MethodInfo对象，将拦截链中的目标方法与之比较</param>
        /// <returns>是否会拦截</returns>
        public static bool WillIntercept(this IInterceptorRegistrationProvider interceptorRegistrationProvider, MethodInfo  methodInfo)
        {
            return interceptorRegistrationProvider.Registrations.Any(it => it.Target == methodInfo);
        }
    }
}
